<?php

use app\common\model\Category;
use fast\Form;
use fast\Tree;
use think\Db;
error_reporting(E_ERROR | ~E_NOTICE);//错误报错级别设置    tp5.0.4以后不支持
function getTableInfo($table,$f_name,$f_value,$field){
    $v=db($table)->where($f_name,$f_value)->value($field);
    return $v;
}

function getTableValue($table,$f_name,$f_value,$field){
    $val=db($table)->where($f_name,$f_value)->value($field);
    return $val;
}

function getTableColumns($table,$f_name,$f_value,$field,$way='eq',$format='arr'){
    $val=db($table)->where($f_name,$way,$f_value)->column($field);
    if($format=='str'){
        $val=implode(',',$val);
    }
    return $val;
}

/**
 * @desc   支付成功后处理订单
 * @name    handleOrder()
 * @param   order_no 订单号,必填
 * @param   method   支付方式         alipay 或   wechat
 * @param   status   订单状态,默认'end'
 * @author  靳邦龙
 * @time    2018-08-13
 */
function handleOrder($order_no,$status='paid'){
    //1、更改订单状态
    $data['order_number'] = $order_no;
    $data['status']=$status;
    $data['updatetime']=time();
    $order_status=  db('hs_order')->where('order_number',$order_no)->update($data);
}
/**
 * @desc   支付成功后处理报名
 * @name    handleEnter()
 * @param   order_no 订单号,必填
 * @param   status   订单状态,默认'end'
 * @author  靳邦龙
 * @time    2018-08-13
 */
function handleApply($order_no,$status='paid'){
    $apply_id=getTableValue('hs_order',"order_number",$order_no,'apply_id');//取enter_id
    //报名状态
    $data['status']=$status;
    $data['updatetime']=time();
    db('hs_recruit_apply')->where('apply_id',$apply_id)->update($data);//修改当前数据 状态
}
/**
 * @desc   支付成功后处理短信
 * @name    handleSms()
 * @param   order_no 订单号,必填
 * @author  靳邦龙
 * @time    2018-08-13
 */
function handleSms($order_no){
    $order=db('hs_order')->where("order_number",$order_no)->find();//1 根据订单号,查询报名记录
    $apply_row=db('hs_recruit_apply')->where("id",$order['apply_id'])->field("uid")->find();//2 取报名记录
    $phone=db('user')->where('id',$apply_row['uid'])->value('mobile');//个人记录
    //发送短信
    aliSend($phone, 'SMS_141606110',json_encode(array('amount'=>$order['order_amount'])) );
}
/**
 * @desc   生成订单
 * @name    createOrder()
 * @param   报名表id,必填        enter_id
 * @param   订单金额,必填          total_amount
 * @author  靳邦龙
 * @time    2018-08-13
 */
function formOrder($apply_id,$total_amount){
    $out_trade_no         = createOrder();//获取订单号
    $data['order_number'] = $out_trade_no;      //订单号
    $data['apply_id']     = $apply_id;          //报名id
    $data['order_amount'] = $total_amount;      //金额
    $data['status']       = 'wait';             //默认 待支付状态
    $data['createtime']   = time();
    $data['updatetime']   = time();
    $res=db('hs_order')->insert($data);         //形成订单
    if($res){
        return $out_trade_no;
    }
}

/**
 * @desc    获取订单号,
 * @name    createOrder()
 * @param   报名表id,必填   enter_id
 * @param   订单金额,必填     amount
 * @author  靳邦龙
 * @time    2018-08-13
 */
function getOrderNO($apply_id,$amount){
    $out_trade_no = getTableValue('hs_order', 'apply_id',$apply_id, 'order_number');//如果已形成订单直接取订单号
    if(!$out_trade_no){
        $out_trade_no = formOrder($apply_id, $amount);//生成订单并返回单号
    }
    return $out_trade_no;
}
/**
 * @desc    获取注册订单订单号,
 * @name    getOrderRegisterNO()
 * @param   注册表单信息  params
 * @param   订单金额,必填     amount
 * @author  靳邦龙
 * @time    2018-08-15
 */
function getOrderRegisterNO($params,$amount){
    $out_trade_no         = createOrder();//获取订单号
    $data['order_no'] = $out_trade_no;      //订单号
    $data['order_amount'] = $amount;      //金额
    $data['status']       = 'wait';             //默认 待支付状态
    $data['createtime']   = time();
    $data['updatetime']   = time();
    $data['params']   = json_encode($params);
    $res=db('hs_order_register')->insert($data);         //形成订单
    if($res){
        return $out_trade_no;
    }
}